//
//  Contoller.m
//  iMAL
//
//  Created by Stefano Pigozzi on 1/30/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Contoller.h"


@implementation Contoller

@synthesize mal;
@synthesize app_delegate;

@synthesize watchingFlag;
@synthesize completedFlag;
@synthesize holdFlag;
@synthesize droppedFlag;
@synthesize planFlag;
@synthesize searchString;

@synthesize sort;


- (id) init
{
	self = [super init];
	if (self != nil) {
		self->sort = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES]];
	}
	return self;
}

- (void) awakeFromNib
{
	
	[self addObserver:self forKeyPath:@"watchingFlag" options:(NSKeyValueObservingOptionNew |
															   NSKeyValueObservingOptionOld) context:NULL];
	[self addObserver:self forKeyPath:@"completedFlag" options:(NSKeyValueObservingOptionNew |
															   NSKeyValueObservingOptionOld) context:NULL];
	[self addObserver:self forKeyPath:@"holdFlag" options:(NSKeyValueObservingOptionNew |
															   NSKeyValueObservingOptionOld) context:NULL];
	[self addObserver:self forKeyPath:@"droppedFlag" options:(NSKeyValueObservingOptionNew |
															   NSKeyValueObservingOptionOld) context:NULL];
	[self addObserver:self forKeyPath:@"planFlag" options:(NSKeyValueObservingOptionNew |
															   NSKeyValueObservingOptionOld) context:NULL];
	[self addObserver:self forKeyPath:@"searchString" options:(NSKeyValueObservingOptionNew |
															   NSKeyValueObservingOptionOld) context:NULL];
	
}


-(void)constructPredicate
{
	NSString * _pred = nil;
	if(watchingFlag){
		if(searchString != nil){
			_pred = @"((status like[c] \"Watching\")";
		} else {
			_pred = @"(status like[c] \"Watching\")";
		}
	}
	if(completedFlag){
		if(_pred != nil){
			_pred = [_pred stringByAppendingString:@" OR (status like[c] \"Completed\")"];
		} else {
			if(searchString != nil){
				_pred = @"((status like[c] \"Completed\")";
			} else {
				_pred = @"(status like[c] \"Completed\")";
			}
		}
	}
	if(holdFlag){
		if(_pred != nil){
			_pred = [_pred stringByAppendingString:@" OR (status like[c] \"On-Hold\")"];
		} else {
			if(searchString != nil){
				_pred = @"((status like[c] \"On-Hold\")";
			} else {
				_pred = @"(status like[c] \"On-Hold\")";
			}
		}
	}
	if(droppedFlag){
		if(_pred != nil){
			_pred = [_pred stringByAppendingString:@" OR (status like[c] \"Dropped\")"];
		} else {
			if(searchString != nil){
				_pred = @"((status like[c] \"Dropped\")";
			} else {
				_pred = @"(status like[c] \"Dropped\")";
			}
		}
	}
	if(planFlag){
		if(_pred != nil){
			_pred = [_pred stringByAppendingString:@" OR (status like[c] \"Plan to Watch\")"];
		} else {
			if(searchString != nil){
				_pred = @"((status like[c] \"Plan to Watch\")";
			} else {
				_pred = @"(status like[c] \"Plan to Watch\")";
			}
		}
	}
	if(searchString != nil){
		if(_pred != nil){
			_pred = [_pred stringByAppendingFormat:@") AND ((title like[c] \"*%@*\") OR (title_alternatives like[c] \"*%@*\"))", searchString, searchString];
		} else {
			_pred = [NSString stringWithFormat:@"(title like[c] \"*%@*\") OR (title_alternatives like[c] \"*%@*\")", searchString, searchString];
		}
	}
	if(_pred != nil){
		NSLog(_pred);
		[animes setFilterPredicate:[NSPredicate predicateWithFormat:_pred]];
	} else {
		[animes setFilterPredicate:[NSPredicate predicateWithFormat:nil]];
	}
	
}

-(IBAction) import: (id) sender
{
	NSError * err;
	[[app_delegate managedObjectContext] save:&err]; // saving to deal with rollback
	mal.progressMaximum = [NSNumber numberWithInt:100];
	mal.progressCurrent = [NSNumber numberWithInt:0];
	mal.progressMessage = @"Preparing for anime import...";
	[self showProgressSheet];
	[mal deep_import];
}

-(void) showProgressSheet
{
	[NSApp beginSheet:progressSheet modalForWindow:mainWindow modalDelegate:nil didEndSelector:nil contextInfo:nil];
}

-(IBAction) cancelImport:(id) sender
{
	[[mal scheduledOperations] cancelAllOperations]; //cancel waiting operations
	[[app_delegate managedObjectContext] rollback]; //rolls back to last save NOTE: save occurs before import.
	[progressSheet orderOut:nil];
	[NSApp endSheet:progressSheet];
}



- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
	if([keyPath isEqual:@"watchingFlag"]||[keyPath isEqual:@"completedFlag"]||[keyPath isEqual:@"holdFlag"]||[keyPath isEqual:@"droppedFlag"]||[keyPath isEqual:@"planFlag"]){
		[self constructPredicate];
	}
	if([keyPath isEqual:@"searchString"]){
		[self constructPredicate];
	}
}

@end
